package com.javasm.demobike;

import java.util.Arrays;
import java.util.Scanner;

public class ShareBikeManager {

    private ShareBikeCompany[] shareBikeCompanies;

    Scanner  scanner = new Scanner(System.in);
    public void menu(){

        init();

        String answer;
        do{

            System.out.println("欢迎进入共享单车管理系统");
            System.out.println("             1投放单车");
            System.out.println("             2查看单车");
            System.out.println("             3删除单车");
            System.out.println("             4借出单车");
            System.out.println("             5归还单车");
            System.out.println("            6排行榜");
            System.out.println("请输入：      ");
            int  num = scanner.nextInt();
            switch (num){
                case 1 :
                    adddddddBike();
                    break;
                case 2:
                    queryyyyyyyyyBike();
                    break;
                case 3:
                    removeBike();
                    break;
                case 4:
                    borrowwwwwwwwBike();
                    break;
                case 5:
                    backkkkkkkkkkBike();
                    break;
                case 6:
                    rank();
                    break;

            }
            System.out.println("是否继续？？？y/n");
            answer = scanner.next();


        }while ("y".equals(answer));
    }

    private void rank() {
        ShareBikeCompany[] shareBikeCompanies1 = Arrays.copyOf(this.shareBikeCompanies, shareBikeCompanies.length);
        Arrays.sort(shareBikeCompanies1,(a,b) -> b.getCount() - a.getCount());
        System.out.println("公司名称|借出次数");
        for (int i = 0; i < shareBikeCompanies1.length; i++) {
            System.out.print(shareBikeCompanies1[i].getName()  + "     ");
            System.out.print(shareBikeCompanies1[i].getCount()  + "     ");
            System.out.println("----------------");
        }

    }

    private void removeBike() {
        int companyNum = getCompanyNum();
        System.out.println("请输入要删除的单车的id");
        int biekId  = scanner.nextInt();
        switch (companyNum){
            case 1:
                shareBikeCompanies[0].removeBike(biekId);
                break;
            case 2:
                shareBikeCompanies[1].removeBike(biekId);
                break;
            case 3:
                shareBikeCompanies[2].removeBike(biekId);
                break;
        }


    }

    private void backkkkkkkkkkBike() {
        int companyNum = getCompanyNum();
        System.out.println("请输入要归还的车辆ID");
        int bikeId = scanner.nextInt();
        switch (companyNum){
            case 1:
                shareBikeCompanies[0].backBike(bikeId);
                break;
            case 2:
                shareBikeCompanies[1].backBike(bikeId);
                break;
            case 3:
                shareBikeCompanies[2].backBike(bikeId);
                break;
        }



    }


    private void borrowwwwwwwwBike() {
        int companyNum = getCompanyNum();
        System.out.println("请输入要借出的车辆ID");
        int bikeId = scanner.nextInt();
        switch (companyNum){
            case 1:
                shareBikeCompanies[0].borrowBike(bikeId);
                break;
            case 2:
                shareBikeCompanies[1].borrowBike(bikeId);
                break;
            case 3:
                shareBikeCompanies[2].borrowBike(bikeId);
                break;
        }

        
        
    }

    private void queryyyyyyyyyBike() {
        int companyNum = getCompanyNum();
        for (int i = 0; i < shareBikeCompanies.length; i++) {
            System.out.println("|公司id|名称|单车数量|借出次数");
            System.out.print(shareBikeCompanies[i].getId()  + "\t");
            System.out.print(shareBikeCompanies[i].getName()  + "\t");
            System.out.print(shareBikeCompanies[i].getSum()  + "\t");
            System.out.print(shareBikeCompanies[i].getCount()  + "\t");
            System.out.println();
            //单车信息
            ShareBike[] shareBikes = shareBikeCompanies[i].getShareBikes();
            System.out.println("单车id|单车状态|借出时间");
            for (int j = 0; j < shareBikeCompanies[i].getSum(); j++) {
                System.out.print(shareBikes[j].getId()+"\t");
                System.out.print(shareBikes[j].getStatus()+"\t");
                System.out.print(shareBikes[j].getBorrowTime()+"\t");
                System.out.println();
            }


        }
        
    }

    private void init() {
        shareBikeCompanies = new ShareBikeCompany[3];
        shareBikeCompanies[0] = new OfoCompany(1,"ofo",new ShareBike[5],0,0);
        shareBikeCompanies[1] = new HaloCompany(2,"Halo",new ShareBike[5],0,0);
        shareBikeCompanies[2] = new MobaiCompany(3,"MoBike",new ShareBike[5],0,0);


    }

    private void adddddddBike() {
        int companyNum = getCompanyNum();
        System.out.println("请输入投放的数量");
        int bikeNum = scanner.nextInt();
        switch (companyNum){
            case 1 :
                shareBikeCompanies[0].addBike(bikeNum);
                break;
            case 2:
                shareBikeCompanies[1].addBike(bikeNum);
                break;
            case 3:
                shareBikeCompanies[2].addBike(bikeNum);
                break;
            default:
                System.out.println("请输入12312312313221312321321312321321321321321312132213");
                break;
        }
    }

    private int getCompanyNum() {
        System.out.println("1 ofoo oooooooo");
        System.out.println("2 halo oooooooo");
        System.out.println("3 mobai ooooooo");
        System.out.println("请选择公司");
        int companyNum = scanner.nextInt();
        return companyNum;
    }


}
